/**
 * Created by HaiHui on 2017/10/17.
 */
/**
 * Created by HaiHui on 2017/10/17.
 */
var table;
$(document).ready(function() {
    table = $('#example').DataTable({
        "pagingType" : "simple_numbers",//设置分页控件的模式
        searching : false,//屏蔽datatales的查询框
        aLengthMenu : [10],//设置一页展示10条记录
        "bLengthChange" : false,//屏蔽tables的一页展示多少条记录的下拉列表
        "ordering" : false,//屏蔽排序
        "oLanguage" : { //对表格国际化
            "sLengthMenu" : "每页显示 _MENU_条",
            "sZeroRecords" : "没有找到符合条件的数据",
            //  "sProcessing": "&lt;img src=’./loading.gif’ /&gt;",
            "sInfo" : "当前第 _START_ - _END_ 条　共计 _TOTAL_ 条",
            "sInfoEmpty" : "木有记录",
            "sInfoFiltered" : "(从 _MAX_ 条记录中过滤)",
            "sSearch" : "搜索：",
            "oPaginate" : {
                "sFirst" : "首页",
                "sPrevious" : "前一页",
                "sNext" : "后一页",
                "sLast" : "尾页"

            }
        },
        "processing" : true, //打开数据加载时的等待效果
        "serverSide" : true,//打开后台分页
        "ajax" : {
            "url" : "dust/list",
            "dataSrc" : "aaData",
            "data" : function(d) {
                d.dustName = $('#selectDustName').val();
                //添加额外的参数传给服务器
                //d.aduserRealname = aduserRealname;
            }
        },
        "columns" : [
            {"data" : "dustId","visible":false},
            {"data" : "dustName","title":"公墓名称"},
            {"data" : "areaName","title":"所属区域"},
            {"data" : "dustPersionName","title":"负责人"},
            {"data" : "dustPersionPhone","title":"联系电话"},
            {"data" : "dustGraveCount","title":"墓穴数"},
            {"data" : "dustOkGraveCount","title":"已安葬数"},
            {"data" : "dustNoGraveCount","title":"空穴数"},

            {"data" : null,"title":"操作"}

        ],
        "columnDefs" : [ {
            "targets" : -1,//操作按钮目标列
            "data" : null,
            "defaultContent": "<i title=\"详情\" class=\"dogo-js-search fa fa-search\"></i><i title=\"编辑\" class=\"dogo-js-edit fa fa-edit\"></i><i title=\"区管理\" class=\"dogo-js-set fa fa-bars\"></i><i title=\"穴管理\" class=\"dogo-js-grave fa fa-tty\"></i><i title=\"删除\" class=\"dogo-js-del fa fa-trash-o\"></i>"

        } ],

    });
    //查看
    $('#example tbody').on('click', '.dogo-js-search', function() {
        var data = table.row($(this).parents('tr')).data();
        showDetail(data.dustId)
        // $('#search-modal').modal(data.prId);
    });
    //del
    $('#example tbody').on('click', '.dogo-js-del', function() {
        var data = table.row($(this).parents('tr')).data();
        var confirmIdx = layer.confirm("确认要删除?", {
                btn: ['删除', '取消']
            },
            function() {
                var setting = {
                    url: "dust/delete",
                    data: {
                        id: data.dustId
                    }
                }
                ajaxSubmit(setting, function(responseData) {
                    checkSubmitResult(responseData, {
                        error: "删除失败"
                    }, function(responseData) {
                        layer.alert(responseData.description);
                        $('#example').DataTable().ajax.reload();

                    });
                }, "删除失败");
            },
            function() {
                layer.close(confirmIdx);
            });

    });
    //edit
    $('#example tbody').on('click', '.dogo-js-edit', function() {

        var data = table.row($(this).parents('tr')).data();
        showEditModal(data.dustId);


    });
    $('#example tbody').on('click','.dogo-js-set',function(){
        var data = table.row($(this).parents('tr')).data();
        var dataId =data.dustId;
        alert(data.dustName);
        $("#infoDustNameByRegion").text(data.dustName);
        $("#regionList").find("li").remove();
        var setting = {
            url: "dust/selectRegions",
            data: {
                id: dataId
            }
        }
        ajaxSubmit(setting, function(responseData) {
            checkSubmitResult(responseData, {
                error: "获取项目详情失败"
            }, function(responseData) {
                //TODO 详情页面数据填充
                var data = responseData.data;

                for(var i = 0; i < data.length; i++ ){
                    var region = data[i];
                    var html =" <li  onmouseover='show(this)' onmouseout='hide(this)' id='"+region.regionId+"'> <p><em class='fa fa-object-group dogo-css-areaem'></em>"+region.regionName+"      <span style='display: none'><i class='fa fa-trash-o'  style='margin-left: 30px;' onclick='deleteRegion("+"this,"+region.regionId+")'></i></span></p></li>";
                    $("#regionList").append(html);
                }


                $("#hideDustId").val(dataId);

                $("#regionModel").modal('show');
            });
        }, "获取项目详情失败");



    });
    //edit
    $('#example tbody').on('click', '.dogo-js-grave', function() {

        var data = table.row($(this).parents('tr')).data();
        window.location.href="graveManager.html?item_nav=dustManager&id="+data.dustId+"&name="+encodeURI(encodeURI(data.dustName));


    });
});
function show(thisObj){
    $(thisObj).children().children("span").show();

}
function hide(thisObj){
    $(thisObj).children().children("span").hide();
}

function search() {
    table.ajax.reload();

}

function showAddModal() {
    $("#modal-label").text("添加公墓信息");
    $("#saveProjectInfo").text("添加");
    $("#saveProjectInfo").data("opType", "add");
    $("#dustId").val("");
    $("#dustName").val("");
    $("#dustPersionName").val("");
    $("#dustRegionId").val("");
    $("#dustPersionPhone").val("");
    $("#dustRemark").val("");
    $('#edit-modal').modal();
    initCombo('dustRegionId', 'area/townList', 'xingzhengBianhao', 'xingzhengMingcheng');
}
function showEditModal(dataId) {
    $("#modal-label").text("修改公墓信息");
    $("#saveProjectInfo").text("修改");
    $("#saveProjectInfo").data("opType", "edit");
    var setting = {
        url: "dust/detail",
        data: {
            id: dataId
        }
    }
    ajaxSubmit(setting, function(responseData) {
        checkSubmitResult(responseData, {
            error: "获取项目详情失败"
        }, function(responseData) {
            //TODO 详情页面数据填充
            var data = responseData.data;
            $("#dustId").val(data.dustId);
            $("#dustName").val(data.dustName);
            $("#dustPersionName").val(data.dustPersionName);
            $("#dustPersionPhone").val(data.dustPersionPhone);
            $("#dustRemark").val(data.dustRemark);
            initCombo('dustRegionId', 'area/townList', 'xingzhengBianhao', 'xingzhengMingcheng',data.dustRegionId);

            $("#edit-modal").modal('show');
        });
    }, "获取项目详情失败");

}
function showDetail(dataId) {
    var setting = {
        url: "dust/detail",

        data: {
            id: dataId
        }
    }
    ajaxSubmit(setting, function(responseData) {
        checkSubmitResult(responseData, {
            error: "获取项目详情失败"
        }, function(responseData) {
            //TODO 详情页面数据填充
            var data = responseData.data;
            $("#search-modal").find("#infoDustName").text(data.dustName);
            $("#search-modal").find("#infoDustRemark").text(data.dustRemark);
            $("#search-modal").find("#infoDustPersionName").text(data.dustPersionName);
            $("#search-modal").find("#infoDustPersionPhone").text(data.dustPersionPhone);
            $("#search-modal").find("#infoDustGraveCount").text(data.dustGraveCount);
            $("#search-modal").find("#infoDustOkGraveCount").text(data.dustOkGraveCount);
            $("#search-modal").find("#infoDustNoGraveCount").text(data.dustNoGraveCount);
            $("#search-modal").find("#infoAreaName").text(data.areaName);
            $("#search-modal").modal('show');
        });
    }, "获取项目详情失败");
}
//添加、修改项目
$("#saveProjectInfo").click(function() {
    var opType = $(this).data("opType");
    var action = null;
    var formdata = {};
    //formdata = new FormData($("#editForm")[0]);

    formdata =  $("#editForm").serializeJson()
    //formdata = $("#editForm").serializeArray();


    var errorMsg = null;

    if(opType == "add") {
        action = "dust/add";
        errorMsg = "添加信息失败";
    } else if(opType == "edit") {
        action = "dust/update";
        errorMsg = "修改信息失败";
    }

    if(action == null) {
        layer.alert("不支持的操作类型");
    } else {

        var setting = {
            url: action,
            data: JSON.stringify(formdata),
            cache: false,
            contentType:"application/json",
            processData: false,
            async: false

        }
        ajaxSubmit(setting, function(responseData) {
            checkSubmitResult(responseData, {
                error: errorMsg
            }, function(responseData) {
                $("#edit-modal").modal('hide');
                layer.alert(responseData.description);
                $('#example').DataTable().ajax.reload();

            });

        }, errorMsg);

    }
});



